/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import fr.ove.openmath.mathematica.*;
import fr.ove.palette.swing.*;

/**
* The Command part.<BR>
* Allows the user to edit, send calculations, etc.
*/
public class PanelCommand extends JPanel implements ChangeListener {
    /**
    * The entry for the formula
    */
    private LinearEntry entry;
    
    /**
    * Flag to indiquate if we have to update the entry value.
    */
    private boolean updateEntry = true; // The default is true. This flag is necessary because
    // when we are typing in the linear form of the formula, once a modification is realized,
    // we set the linear value typed to the current instance in the worksheet which send a event to
    // notify a change. The instance receives the event and tries to update the entry value.
    // However, at this point (worksheet event treatment), we are still in the document listener
    // which has locked the document (entry) value. This causes an exception. So we set the value
    // to false to forbide entry's value change.
    
    /**
    * The current element to edit.
    */
    private OMWorkSheet worksheet;
    
    /**
    * The OpenMath viewer.
    */
    private OpenMathViewer omViewer;
    
    /**
    * The Constructor.
    */
    public PanelCommand(OpenMathViewer viewer, JPaletteEditor palette) {
        setLayout(new BorderLayout());
        omViewer = viewer;
        entry = new LinearEntry();
        palette.addPaletteListener(entry);
        entry.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    worksheet.runCurrentRequest();
                    worksheet.addNewElement();
                    entry.setText("");
            		// pour le moment
            		worksheet.validate();
                }
            }
        );
        
        Document document = entry.getDocument();
        document.addDocumentListener(
            new DocumentListener() {
                public void changedUpdate(DocumentEvent e) {
                }
                
                public void insertUpdate(DocumentEvent e) {
                    Document doc = e.getDocument();
                    try {
                        updateEntry = false;
                        String linear = doc.getText(0, doc.getLength());
                        worksheet.getCurrentElement().setLinearRequest(linear);
                        worksheet.validate();
                        omViewer.setOpenMathRequest(worksheet.getCurrentElement().getOpenMathRequest());
                        updateEntry = true;
                    }
                    catch (BadLocationException ble) {
                        ble.printStackTrace();
                    }
                }
                
                public void removeUpdate(DocumentEvent e) {
                    Document doc = e.getDocument();
                    try {
                        updateEntry = false;
                        String linear = doc.getText(0, doc.getLength());
                        worksheet.getCurrentElement().setLinearRequest(linear);
                        worksheet.validate();
                        omViewer.setOpenMathRequest(worksheet.getCurrentElement().getOpenMathRequest());
                        updateEntry = true;
                    }
                    catch (BadLocationException ble) {
                        ble.printStackTrace();
                    }
                }
            }
        );
        
        add(BorderLayout.CENTER, entry);
        
        setVisible(false);
    }
    
    /**
    * Clear the entry.
    */
    public void clear() {
        entry.setText("");
    }
    
    //  ### ChangeListenerInterface implementation
    
    /**
    * Invoked when the target of the listener has changed its state.
    * @param e a ChangeEvent object 
    */
    public void stateChanged(ChangeEvent e) {
        WorkBook wBook = (WorkBook) e.getSource();
        int count = wBook.getTabCount();
        if (count == 0) {
            setVisible(false);
            worksheet = null;
        }
        else {
            setVisible(true);
            JInternalFrame jif = wBook.getActiveInternalFrame();
            
            Container contentPane = jif.getContentPane();
            if (contentPane instanceof OMWorkSheet) {
                worksheet = (OMWorkSheet) jif.getContentPane();
                String linearRequest = worksheet.getCurrentElement().getLinearRequest();
                if (updateEntry)
                    entry.setText(linearRequest);
            }
        }
    }
}
